﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Module.TCenter
{
    public partial class MyTCenter : System.Web.UI.Page
    {
        private int userId;
        private int cUserId;
        private int TCenterId;
        protected string ModuleId;
        private Jy123.Model.TCenter_Main TCenter;
        private Jy123.Model.Module_Main module;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;
        private List<Jy123.Model.Club_Main> listFriendClub;
        private TimeSpan span = TimeSpan.FromHours(2);
        private string[] strContent = { "我的中心", "中心升级", "训练设备", "组件排行", "训练信息", "组件维护", "商业活动" };
        private string[] strCss = { "lmenu1", "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu2" };
        private string[] strHeaderCss = { "", "", "NavIcon", "" };

        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            if (!Page.IsPostBack)
            {
                cUserId = int.Parse(strUserIdFromCookie);
                LoadInitialValue();
            }
        }

        private void LoadInitialValue()
        {
            string key = "TCenterId";
            string venuesIdString = Request.QueryString[key];
            if (string.IsNullOrEmpty(venuesIdString))
            {
                venuesIdString = "1";
            }
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                TCenterId = int.Parse(venuesIdString);
                ModuleId = venuesIdString;
                Jy123.BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                Jy123.BLL.Module.Module BLLModule = new Jy123.BLL.Module.Module();
                Jy123.BLL.Module_Main BLLModuleMain = new Jy123.BLL.Module_Main();
                Jy123.BLL.TCenter.TModule BLLTModule = new Jy123.BLL.TCenter.TModule();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                TCenter = BLLTCenter.GetModel(TCenterId);

                userId = TCenter.FUser.Value;
                user = BLLUser.GetModel(userId);
                level = BLLLevel.GetModel(TCenter.FLevel.Value);
                module = BLLModuleMain.GetModel(TCenterId);
                listFriendClub = BLLTModule.FriendsClub(userId, true);
                List<Model.Club_Main> listAllClub = BLLTModule.FriendsClub(userId, false);
                Dictionary<int, Model.Club_Main> dicClub = new Dictionary<int, Jy123.Model.Club_Main>();
                foreach (Model.Club_Main cClub in listAllClub)
                {
                    dicClub.Add(cClub.ID, cClub);
                }
                Calculate(TCenter, dicClub);//计算已经超过训练时间的俱乐部
                BindHTML();
            }
            else
            {
                //找不到UserId，返回首页,目前暂时返回空值
            }
        }

        private void Calculate(Model.TCenter_Main TCenter, Dictionary<int, Model.Club_Main> dicClub)
        {
            BLL.TCenter_ClubTC BLLTC = new Jy123.BLL.TCenter_ClubTC();
            string strWhere = string.Format("FTCenter = {0} AND TrainingTime IS NULL", TCenter.ID.ToString());
            List<Model.TCenter_ClubTC> listTC = BLLTC.GetModelList(strWhere);
            Dictionary<int, Model.TCenter_ClubTC> dicTC = new Dictionary<int, Jy123.Model.TCenter_ClubTC>();
            foreach (Model.TCenter_ClubTC TC in listTC)
            {
                dicTC.Add(TC.FClub.Value, TC);
            }
            string[] strClub = TCenter.TrainClub.Split(',');
            for (int i = 0; i < strClub.Length; i++)
            {
                if (!string.IsNullOrEmpty(strClub[i]))
                {
                    int cId = int.Parse(strClub[i]);
                    Model.Club_Main cClub = dicClub[cId];
                    Model.TCenter_ClubTC cTC = dicTC[cClub.ID];
                    DateTime startTime = cTC.StartTime.Value;
                    BLL.TCenter.TModule BLLTModule = new Jy123.BLL.TCenter.TModule();
                    if (DateTime.Now.Subtract(span) > startTime)
                    {
                        BLLTModule.QuitTCenter(cClub, TCenter);
                    }
                }

            }
        }

        private void BindHTML()
        {
            string strHTML = Cache["mytcenter"].ToString();
            Jy123.Web.AppCode.Module appModule = new Jy123.Web.AppCode.Module();
            string strHeader = Cache["header"].ToString();
            string headerString = appModule.BindHeader(strHeader, strHeaderCss);
            string strLeft = Cache["leftmenu"].ToString();
            string strLeftLi = Cache["leftmenuli"].ToString();
            string strTail = "?TCenterId=" + TCenter.ID.ToString();
            string[] strHref = { "#", "UpdateTCenter.aspx" + strTail, "TCenterAnnexe.aspx" + strTail, "TCenterRank.aspx" + strTail, "TrainInfo.aspx" + strTail, "TCenterRepair.aspx" + strTail, "TCenterHire.aspx" + strTail };
            string[] strNoRaceHref = { "#", "UpdateTCenter.aspx" + strTail, "TCenterAnnexe.aspx" + strTail, "TCenterRank.aspx" + strTail, "javascript:$.moonbox.warning('您的训练中心没有进行俱乐部进行训练！')", "TCenterRepair.aspx" + strTail, "TCenterHire.aspx" + strTail };
            string leftMenuString;
            if (TCenter.CurrentClubs.Value > 0)
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strHref, strCss);
            }
            else
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strNoRaceHref, strCss);
            }
            string strClub = Cache["contentclub"].ToString();
            string contentString = appModule.BindContentClub(strClub, module, TCenter, user, level);
            string strMain = Cache["mainmenu"].ToString();
            string strDiv = Cache["mainmenusubdiv"].ToString();
            string strLi = Cache["mainmenusubdivli"].ToString();
            string mainMenuString = appModule.BindMainMenu(strMain, strDiv, strLi, cUserId);
            string strClubList = Cache["clublist"].ToString();
            string clubListString = appModule.BindClubList(strClubList, userId, listFriendClub);
            strHTML = strHTML.Replace("$Header$", headerString);
            strHTML = strHTML.Replace("$MainMenu$", mainMenuString);
            if (cUserId == userId)
            {
                strHTML = strHTML.Replace("$LeftMenu$", leftMenuString);
            }
            else
            {
                strHTML = strHTML.Replace("$LeftMenu$", string.Empty);
            }
            strHTML = strHTML.Replace("$ContentClub$", contentString);
            strHTML = strHTML.Replace("$ClubList$", clubListString);
            strHTML = strHTML.Replace("$Footer$", Cache["footer"].ToString());
            strHTML = strHTML.Replace("$ModuleId$", TCenter.ID.ToString());
            string strObject = Cache["flashobject"].ToString();
            //string strXML = string.Format("/Module/Venues/GetFlashXml.ashx?ModuleId={0}", TCenter.ID);
            string strXML = string.Format("/Module/Venues/XML/{0}/{1}.xml", TCenter.ID, Guid.NewGuid());
            strObject = strObject.Replace("$XML$", strXML);
            strObject = strObject.Replace("$Value$", "/Cache/Module/Flash/Court.swf");
            strHTML = strHTML.Replace("$Object$", strObject);

            strHTML = strHTML.Replace("$ClubId$", TCenter.TrainClub);
            strHTML = strHTML.Replace("$Count$", TCenter.CurrentClubs.Value.ToString());
            string mType = "Normal";
            strHTML = strHTML.Replace("$MaxCount$", TCenter.Capacity.Value.ToString());
            strHTML = strHTML.Replace("$Count$", mType);
            Response.Write(strHTML);
        }
    }
}
